package com.example.administrator.tvfoucs;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.tvfoucs.data.ItemDatas;
import com.example.administrator.tvfoucs.tvrecylerview.widget.SimpleOnItemListener;
import com.example.administrator.tvfoucs.tvrecylerview.widget.TvRecyclerView;
import com.owen.focus.AbsFocusBorder;
import com.owen.focus.FocusBorder;

import java.util.List;


public class TvRecylerviewActivity extends AppCompatActivity {


    private TvRecyclerView tvRecyclerView;
    private List<String> movietDatas;
    private FocusBorder focusBorder;
    //测试我的主分支，不能动哦
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv);
        tvRecyclerView = findViewById(R.id.list);
        tvRecyclerView.setSpacingWithMargins(getResources().getDimensionPixelOffset(R.dimen.y_tv_6), getResources().getDimensionPixelOffset(R.dimen.x_tv_6));
        tvRecyclerView.setSelectedItemAtCentered(true);
        initData();
        GridAdapter gridAdapter = new GridAdapter(this, movietDatas);
        //增加item之间的间距
//        tvRecyclerView.setSpacingWithMargins(20, 40);
//        tvRecyclerView.setSelectedItemAtCentered(true);

        tvRecyclerView.setAdapter(gridAdapter);
        /** 图片焦点框 */
        focusBorder = new FocusBorder.Builder().asDrawable()
                .borderDrawableRes(R.drawable.focus)
                //标题相关配置
//                .titleBackgroundRes(R.drawable.shape_item_title_bg)
//                .titleMarginBottomAutoAlignBorder()
//                .titleMargin(10, 0, 10, 4)
////                .titlePadding(10)
//                .titleWidth(ViewGroup.LayoutParams.MATCH_PARENT)
//                .titleTextColor(Color.LTGRAY)
//                .titleTextSize(24)
                //边框动画模式【飞框效果】
                .animMode(AbsFocusBorder.Mode.TOGETHER)
                .build(this);
        tvRecyclerView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (focusBorder != null) {
                    focusBorder.setVisible(hasFocus);
                }
            }
        });


        focusBorder.boundGlobalFocusListener(new FocusBorder.OnFocusCallback() {
            @Nullable
            @Override
            public FocusBorder.Options onFocus(View oldFocus, View newFocus) {
                return focus(oldFocus, newFocus);
            }
        });

        setLinsener();
    }

    private void setLinsener() {
        tvRecyclerView.setOnItemListener(listener);

    }

    private void initData() {
        movietDatas = ItemDatas.getMovietData(100);
    }

    protected FocusBorder.Options focus(View oldFocus, View newFocus) {
//        if (newFocus instanceof TvGridLayout)
//            return null;
        return FocusBorder.OptionsFactory.get(1.2f, 1.2f);
    }

    SimpleOnItemListener listener = new SimpleOnItemListener() {

        @Override
        public void onItemSelected(TvRecyclerView parent, View itemView, int position) {

            onMoveFocusBorder(itemView, 1.1f, 20);
        }

        @Override
        public void onItemClick(TvRecyclerView parent, View itemView, int position) {
            Toast.makeText(TvRecylerviewActivity.this,"position "+position,Toast.LENGTH_SHORT).show();
        }
    };

    protected void onMoveFocusBorder(View focusedView, float scale, float roundRadius) {
        if (null != focusBorder) {
            focusBorder.onFocus(focusedView, FocusBorder.OptionsFactory.get(1.2f, 1.2f, 0));
        }
    }
}
